package cn.dxh.stagefinal.service.impl;

import cn.dxh.stagefinal.bean.Business;
import cn.dxh.stagefinal.bean.Employee;
import cn.dxh.stagefinal.bean.Matter;
import cn.dxh.stagefinal.bean.Vehicle;
import cn.dxh.stagefinal.dao.BusinessDao;
import cn.dxh.stagefinal.dao.MatterDao;
import cn.dxh.stagefinal.service.BusinessService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author: Panjianwei
 * @date: 2019/12/10 15:08
 * 代码是一门艺术，需要我们细细用心品味！
 */
@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessDao businessDao;
    @Autowired
    private MatterDao matterDao;
    @Override
    public List<Vehicle> findVehicleAll() {
        return businessDao.findVehicleAll();
    }
    @Override
    public int addBusiness(Business business,HttpSession session) {
        Employee currentUser = (Employee)session.getAttribute("currentUser");
        String s1 = currentUser.geteName();
        business.setRecordName(s1);
        business.setRecorddate(new Date());
        return businessDao.addBusiness(business);
    }
    @Override
    public PageInfo<Business> findBusinessAll(int itemsid, String relation, String recorddate, int account,int pageindex,int pagesize) {

        Map map = new HashMap();
        map.put("itemsid",itemsid);
        map.put("relation",relation);
        map.put("recorddate",recorddate);
        map.put("account",account);
//      指定分页参数
        PageHelper.startPage(pageindex,pagesize);
        List<Business> businessAll = businessDao.findBusinessAll(map);

        PageInfo<Business> pageInfo = new PageInfo<>(businessAll);
        return pageInfo;
    }

    @Override
    public Business findbybid(int bid) {
        return businessDao.findbybid(bid);
    }

    @Override
    public int udpatebusiness(Business business) {
        return businessDao.udpatebusiness(business);
    }

    @Override
    public int updatestatu(int bid) {
        return businessDao.updatestatu(bid);
    }

    @Override
    public int updatecheck(int bid) {
        return businessDao.updatecheck(bid);
    }

    @Override
    public List<Float> math() {
        float sum=0,sum1=0;
        Map map = new HashMap();
        map.put("itemsid",-1);
        map.put("relation","");
        map.put("recorddate","");
        map.put("account",-1);
        ArrayList<Float> arrayList = new ArrayList();
        List<Business> list = businessDao.findBusinessAll(map);
        for (Business business : list) {

            if(business.getAccount()==0)
            {
                sum+=Float.parseFloat(business.getMoney());
            }
            if(business.getAccount()==1)
            {
                sum1+=Float.parseFloat(business.getMoney());
            }
        }
        arrayList.add(sum);
        arrayList.add(sum1);
        return  arrayList;
    }

}
